<?php


namespace app\api\controller\v1;

use app\common\model\OrderGoods;
use app\common\model\Verification as VerificationModel;
use app\common\model\Members as MembersModel;
use app\common\model\VerificationLog;
use app\common\model\Orders as OrdersModel;
use think\Db;

class Verification extends Base
{
    //绑定核销员
    public function bindingVerificationInit(){
        Db::startTrans();
        try {
            $user_id = $this->checkUserToken();
            $verification_code_id = input('verification_code_id');
            $data = VerificationModel::get($verification_code_id);
            if (empty($data)){
                return j(1,'二维码已过期');
            }
            if ($data['is_binding']){
                return j(1,'该二维码已绑定核销员');
            }
            $member = MembersModel::get($user_id);
            if ($member['is_binding_verification']){
                return j(1,'您已成为核销员,请勿重复绑定');
            }
            $member->is_binding_verification = 1;
            if ($member->save()){
                $data->member_id = $member['id'];
                $data->is_binding = 1;
                if ($data->save()){
                    Db::commit();
                    return j(0,"绑定成功");
                }
            }
            Db::rollback();
            return j(1,"修改用户失败");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return j(1,"操作失败");
        }
    }
    //核销票务初始化
    public function verificationTicketingInit(){
        $user_id = $this->checkUserToken();
//        $member = MembersModel::get($user_id);
//        if (!$member['is_binding_verification']){
//            return j(1,'无权限访问');
//        }
        $order_goods_id = input('order_goods_id');
        $data = OrderGoods::get($order_goods_id)->append(['member_info']);
        return j(0,'获取数据成功',$data);
    }
    //核销票务
    public function verificationTicketing(){
//                $user_id = input('user_id');
        $user_id = $this->checkUserToken();
        $member = MembersModel::get($user_id);
        $order_goods_id = input('order_goods_id');
        if (!$member['is_binding_verification']){
            return j(1,'无权限访问');
        }
        $verificationUser = VerificationModel::where("member_id",$user_id)->field('id')->find();
        if (empty($verificationUser)){
            return j(1,'该用户未绑定核销员');
        }
        Db::startTrans();
        try {
            $orderGood = OrderGoods::get($order_goods_id);
            if ($orderGood['verification_status']){
                return j(1,"该票务已被核销");
            }
            $orderGood->verification_status = 1;
            if ($orderGood->save()){
                if ($this->saveVerificationLog($verificationUser['id'],$user_id,$order_goods_id,$orderGood['order_id'],$orderGood['goods_id'])){
                    if ($this->orderComplete($orderGood['order_id'])){
                        Db::commit();
                        return j(0,"核销成功");
                    }
                    Db::rollback();
                    return j(1,"修改订单状态失败");
                }
            }
            Db::rollback();
            return j(1,"修改核销状态或添加核销记录失败");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return j(1,"操作失败");
        }
    }
    public function saveVerificationLog($verification_id,$member_id,$order_goods_id,$order_id,$goods_id){
        $verificationLog = new VerificationLog();
        $verificationLog->verification_id = $verification_id;
        $verificationLog->member_id = $member_id;
        $verificationLog->order_goods_id = $order_goods_id;
        $verificationLog->order_id = $order_id;
        $verificationLog->goods_id = $goods_id;
        if($verificationLog->save()){
            return true;
        }
        return  false;
    }
    public function orderComplete($order_id){
        $orderGoodsCount = OrderGoods::where("order_id",$order_id)->count('id');
        $verificationLogCount = VerificationLog::where("order_id",$order_id)->count('id');
        //修改订单状态
        if ($orderGoodsCount == $verificationLogCount){
            $order = OrdersModel::get($order_id);
            $order->shipping_status = 2;
            $order->order_status    = 255;
            $order->sign_time       = time();
            $order->finish_time     = time();
            if ($order->save()){
                return true;
            }
            return false;
        }
        return true;
    }
}